<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\User;
class Login extends Controller
{
    protected function _initialize()
    {
        if(!is_file(APP_PATH.'database.php'))
        {
            exit();
        }
    }      
   public function index($username=null,$password=null)
    {
    	if($this->request->isPost())
    	{
    		if(empty($username)||empty($password))
    		{
    			$this->error('用户名和密码不能为空');
    		}
    		else
    		{
    			$user = User::get(['username'=>$username]);
    			if($user != null)
    			{
    				if($user->password==md5($password))
    				{
    					#登录成功缓存用户信息并跳转到首页
    					session('username',$user->username);
    					$this->redirect('Index/index');
    				}
    				else
    				{
    					$this->error('登录失败请重试');
    				}
    			}
    			else
    			{
    				$this->error('登录失败请重试');
    			}
    		}
    	}
    	else
    	{
    		return $this->fetch();
    	}    	        
    }
}
